using System;
using System.Collections.Generic;
using System.IO;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.Graphics.Drawable;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Util;
using Android.Views;
using Android.Widget;
using mb;
using mb.Droid;
using mb.DroidLib;
using static Android.Views.MotionEvent;
using Orientation = Android.Widget.Orientation;

namespace libmp
{
    using MenuItemList = List < (string name, Action handler) >;

    public static class Common
    {
        static ILog logger = App.logger;

        public static int TitleBarHeight = 120;

        public class TitleBarOption
        {
            public string Title;
            public MenuItemList MenuItems;
        }
        public static View CreateTitleBar(TitleBarOption option)
        {
            var activity = StartActivity.This;
            var rootLayout = new RelativeLayout(activity);
            rootLayout.LayoutParameters = new ViewGroup.LayoutParams(-1, TitleBarHeight);
            rootLayout.SetBackgroundColor(Util.MainColor);

            //左侧返回
            rootLayout.AddView(
                new LinearLayout(activity)
                {
                    Orientation = Orientation.Horizontal,
                }
                .SetGravityEx(GravityFlags.CenterVertical)
                .SetClickHandler((s, e) =>
                {
                    activity.SupportFragmentManager.PopBackStack();
                })
                .AddViewEx(
                    Util.MakeImageView("user/Fanhui.png"),
                    new LinearLayout.LayoutParams(40, 40) { LeftMargin = 10, RightMargin = 30, }
                )
                .AddViewEx(
                    new TextView(activity) { Text = "返回", }
                    .SetTextColorEx(Color.White)
                    .SetTypefaceEx(Typeface.Default, TypefaceStyle.Bold)
                    .SetTextSizeEx(ComplexUnitType.Pt, 7)
                ),

                new RelativeLayout.LayoutParams(-2, -2)
                .AddRuleEx(LayoutRules.CenterVertical)
                .AddRuleEx(LayoutRules.AlignParentLeft)
            );

            //中间标题
            rootLayout.AddView(
                new TextView(activity) { Text = option.Title, }
                .SetTextColorEx(Color.White)
                .SetTypefaceEx(Typeface.Default, TypefaceStyle.Bold)
                .SetTextSizeEx(ComplexUnitType.Pt, 9),
                new RelativeLayout.LayoutParams(-2, -2).AddRuleEx(LayoutRules.CenterInParent)
            );

            //右侧菜单
            if (option.MenuItems != null)
            {
                rootLayout.AddView(
                    Util.MakeImageView("category/Gengduo2.png")
                    .SetClickHandler((s, e) => { ShowPopMenu(s as View, option.MenuItems); }),

                    new RelativeLayout.LayoutParams(-2, -2) { RightMargin = 20, }
                    .AddRuleEx(LayoutRules.AlignParentRight)
                    .AddRuleEx(LayoutRules.CenterVertical)
                );

            }

            return rootLayout;
        }

        public static void ShowPopMenu(View anchor, MenuItemList menuItems)
        {
            var activity = StartActivity.This;
            var menu = new Android.Support.V7.Widget.PopupMenu(activity, anchor);
            for (int i = 0; i < menuItems.Count; i++)
            {
                menu.Menu.Add(0, i, 0, menuItems[i].name);
            };
            menu.Show();
            menu.MenuItemClick += (s, e) =>
            {
                logger.Log("click", e.Item.ItemId, e.Item.TitleFormatted);
                menuItems[e.Item.ItemId].handler();
            };
        }
    }
}